package com.hgk.wanwan.main.fragment.persentfragment.alarmclock;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.WindowManager;

import width.hgk.wanwan.R;

public class Alarm extends Activity {
	private DatabaseHelper dbHelper;
	String mtime = "";
	String mtext = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState); 
		//������Ϊ��������ʱҲ����������
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
		getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
                                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		// ��������
		Intent intentSV = new Intent(Alarm.this, MyService.class);
		startService(intentSV);
		
		dbHelper = new DatabaseHelper(Alarm.this, "ideal.sql");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query("user", null, "_id=?",
				new String[] { AlarmActivity.Alarming_index + "" }, null, null,
				null);
		if (cursor.moveToFirst()) {
			mtime = cursor.getString(cursor.getColumnIndex("mtime"));
			mtext = cursor.getString(cursor.getColumnIndex("mtext"));
		}
		System.out.println("mtime-->"+mtime+"mtext-->"+mtext);
		db.close();
		addData();//д����ʷ��¼����
		deleteData(AlarmActivity.Alarming_index);//ɾ��ԭ����¼
		
		new AlertDialog.Builder(Alarm.this)
				.setIcon(R.drawable.bell)
				.setTitle("It's time to do something.")
				.setMessage(mtext)
				.setPositiveButton("Done",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								deleteData(AlarmActivity.Alarming_index);//�ٴ�ɾ��ȷ��
								//�ر�������
								Intent intentSV = new Intent(Alarm.this, MyService.class);
								stopService(intentSV);
								//��������widget	
								Intent mWidgetIntent = new Intent();
								mWidgetIntent.setAction("com.ideal.note.widget");
								Alarm.this.sendBroadcast(mWidgetIntent);
								Alarm.this.finish();
							}
						}).show();
		//��������widget	
		Intent mWidgetIntent = new Intent();
		mWidgetIntent.setAction("com.ideal.note.widget");
		Alarm.this.sendBroadcast(mWidgetIntent);
	}
	
	//���ؼ�ʱ�ص���Acitvity
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent intent = new Intent(Alarm.this, AlarmActivity.class);
		startActivity(intent);
		Alarm.this.finish();
	}


	public void addData() {
		ContentValues values = new ContentValues();
		values.put("mtime", mtime);
		values.put("mtext", mtext);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.insert("user_done", null, values);
		db.close();
	}

	public void deleteData(int index) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		// �ӱ���ɾ��ָ����һ�����
		db.execSQL("DELETE FROM " + "user" + " WHERE _id="
				+ Integer.toString(index));
		db.close();
	}
}